Import mojo

Import diddy
Import diddy.functions
Import inputbutton



Class NumberButtons
	Const NO_ANSWER:INT = 999
	Const INPUT_BOX_HEIGHT = 75

	Field x:float
	Field y:float
	Field width
	Field height
	Field buttonWidth
	Field buttonHeight
	Field highlightNumber = 0
	Field pressedNumber:int = -1
	Field rows:Int = 5
	Field columns:Int = 3
	Field inputNumber:String
	Field currentSign:String
	Field currentNumber:String
	Field images:GameImage
	Field buttonsList:ArrayList<InputButton> = New ArrayList<InputButton>
	Field answer:Int = NO_ANSWER
	Field buttonPlus:InputButton
	Field buttonMinus:InputButton
	Field buttonMultiply:InputButton 
	Field buttonDivide:InputButton
	Field buttonDelete:InputButton

	Method New(x,y, _width:Int, _height:Int)
		Local number:Int = 0
		Local rowIncrement:Float = (_height- INPUT_BOX_HEIGHT)/rows
		Local columnIncrement:Float = _width/columns
		
		self.x = x
		self.y = y
		width = _width
		height = _height	
		
		buttonHeight = 65
		buttonWidth = 65
	
		For Local rowY:Int = 1 To rows
			For Local columnX:Int = 1 To columns
			
				if(columnX<>columns)		
					Local button1:InputButton = InputButton.Create(game.images.Find("numbersStrip"),(columnX*columnIncrement)-(columnIncrement/2)+x, (rowY*rowIncrement)-(rowIncrement/2)+y + INPUT_BOX_HEIGHT, New NumberButton(number, self), number, buttonsList)
	
					button1.hoverImage = 16
					number += 1
				else			
					Select rowY
						Case 1
							buttonPlus = InputButton.Create(game.images.Find("numbersStrip"),(columnX*columnIncrement)-(columnIncrement/2)+x, (rowY*rowIncrement)-(rowIncrement/2)+y + INPUT_BOX_HEIGHT, New SignButton("+", self),11, buttonsList)
							buttonPlus.hoverImage = 16
						Case 2
							buttonMinus = InputButton.Create(game.images.Find("numbersStrip"),(columnX*columnIncrement)-(columnIncrement/2)+x, (rowY*rowIncrement)-(rowIncrement/2)+y + INPUT_BOX_HEIGHT, New SignButton("-", self),10, buttonsList)
							buttonMinus.hoverImage = 16
						Case 3
							buttonMultiply = InputButton.Create(game.images.Find("numbersStrip"),(columnX*columnIncrement)-(columnIncrement/2)+x, (rowY*rowIncrement)-(rowIncrement/2)+y + INPUT_BOX_HEIGHT, New SignButton("X", self),15, buttonsList)
							buttonMultiply.hoverImage = 16
						Case 4
							buttonDivide = InputButton.Create(game.images.Find("numbersStrip"),(columnX*columnIncrement)-(columnIncrement/2)+x, (rowY*rowIncrement)-(rowIncrement/2)+y + INPUT_BOX_HEIGHT, New SignButton("/", self),14, buttonsList)
							buttonDivide.hoverImage = 16
						Case 5
							buttonDelete = InputButton.Create(game.images.Find("numbersStrip"), (columnX*columnIncrement)-(columnIncrement/2)+x, (rowY*rowIncrement)-(rowIncrement/2)+y + INPUT_BOX_HEIGHT, New ActionDeleteButton(self),13, buttonsList)
							buttonDelete.hoverImage = 18
					End
				EndIf
			Next
		Next
		
		Local buttonGo:InputButton = InputButton.Create(game.images.Find("numbersStrip"), x+width-buttonWidth/2, buttonHeight/2,  New ActionEqualsButton(self),12, buttonsList)
		buttonGo.hoverImage = 17

	End
	
	Method Update:void()
		InputButton.UpdateAll(buttonsList)
	End
	
	Method Render()	
		'
		' Maths box
		 '
		SetColor 20,20,20
		SetAlpha 1
		DrawRect(x,y, width, INPUT_BOX_HEIGHT )
	'	SetAlpha (1)


		'We draw a text caret every half second:
		if (Millisecs() Mod 1000 < 500) Then
			SetColor(255,255,255)
			DrawRect(x+1, y+20, 7, 3)
		EndIf
		
		SetColor 255,255,255
		if(answer<>NO_ANSWER)		
			_model.font.DrawText(answer, x+5, 10)
		else
			_model.font.DrawText(currentNumber+ " " +currentSign+ " " +inputNumber,  x+5, 10)
		endif
		
		PushMatrix	
		SetAlpha (1)
		SetColor(100,100,255)
		DrawRect(x,y+INPUT_BOX_HEIGHT, width-70, height )
			SetColor(50,50,255)
		DrawRect(x+width-70,y+INPUT_BOX_HEIGHT, 70, height )
		PopMatrix
		InputButton.DrawAll(buttonsList)
	End
	
	Method Clear:Void()
		currentSign = ""
		inputNumber = ""
		answer=NO_ANSWER
	End
	Method GetInputAsString:String(_input:Int)
	
	Select _input
		Case 10
			Return "0"
		Case 11
			Return "+"
		Case 12
			Return "-"
		Case 13
			Return "GO!"
		Case 14
			Return "C"	
		Case 15
			Return ""		
		Default
			Return _input
	End
	
	End
	
End

Class NumberButton Extends ButtonEvent
	Field symbol:Int
	Field parent:NumberButtons
	
	Method New(symbol:Int, parent:NumberButtons)
		Self.symbol=symbol
		Self.parent = parent
	End
	
	Method OnClicked:Void()
		if(parent.inputNumber.Length() <2) Then		
			parent.inputNumber += symbol
		Endif
	End
End

Class SignButton Extends ButtonEvent
	Field sign:String
	Field parent:NumberButtons
	
	Method New(sign:String, parent:NumberButtons)
		Self.sign=sign
		Self.parent = parent
	End
	
	Method OnClicked:Void()
		parent.currentSign = sign
	End
End

Class ActionEqualsButton Extends ButtonEvent
	Field parent:NumberButtons
	
	Method New( parent:NumberButtons)
		Self.parent = parent
	End
	
	Method OnClicked:Void()
		If(parent.currentSign="")
			'warn?'		
			parent.answer = parent.NO_ANSWER
		ElseIf(parent.inputNumber="")
			parent.answer = parent.NO_ANSWER		
		ElseIf(parent.currentSign="+")
			parent.answer = Int(parent.currentNumber)+Int(parent.inputNumber)
		ElseIf(parent.currentSign="X")
			parent.answer = Int(parent.currentNumber)*Int(parent.inputNumber)
		ElseIf(parent.currentSign="/")
			parent.answer = Int(parent.currentNumber)/Int(parent.inputNumber)
		Else
			parent.answer = Int(parent.currentNumber)-Int(parent.inputNumber)
		EndIf
		
	End
End

Class ActionDeleteButton Extends ButtonEvent
	Field parent:NumberButtons
	
	Method New( parent:NumberButtons)
		Self.parent = parent
	End
	
	Method OnClicked:Void()
		If parent.inputNumber.Length()>0
			parent.inputNumber=parent.inputNumber[..parent.inputNumber.Length()-1]
		Else
			parent.Clear()
		Endif
	End
End








